package net.gzw1993.runmate.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import net.gzw1993.runmate.R;
import net.gzw1993.runmate.activity.RecorderActivity;
import net.gzw1993.runmate.activity.SetActivity;
import net.gzw1993.runmate.bean.UserInfo;
import net.gzw1993.runmate.constant.UrlInfo;
import net.gzw1993.runmate.utils.MyBitmapUtilsHelper;


public class MineFragment extends BaseFragment {

    public static final int MAX_COUNT = 3;
    @ViewInject(R.id.mine_vp_runcount)
    private ViewPager vpRuncount;
    @ViewInject(R.id.mine_iv_icon)
    private ImageView ivIcon;

    @ViewInject(R.id.mine_tv_name)
    private TextView tvName;
    @ViewInject(R.id.mine_tv_runMiles)
    private TextView tvRunMiles;
    @ViewInject(R.id.mine_tv_totalCalories)
    private TextView tvTotalCalories;
    @ViewInject(R.id.mine_tv_totalTime)
    private TextView tvTotalTime;
    @ViewInject(R.id.mine_tv_userNumber)
    private TextView tvUserNumber;
    private HttpUtils httpUtils = new HttpUtils();
    private UserInfo userInfo = new UserInfo();

    public MineFragment() {
        // Required empty public constructor
    }

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        ViewUtils.inject(this, view);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
        vpRuncount.setAdapter(adapter);
        setUserInfo();
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.frag_mine, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_settings) {
            Intent intent = new Intent(getActivity(), SetActivity.class);
            getActivity().startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 请求网络，获得用户的信息
     */
    private void setUserInfo() {
        httpUtils.send(HttpRequest.HttpMethod.GET, UrlInfo.getUserInfo(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                userInfo = JSON.parseObject(responseInfo.result, UserInfo.class);
                tvName.setText(userInfo.getName());
                tvRunMiles.setText(userInfo.getRunMiles() == null ? 0 + ""
                        : userInfo.getRunMiles().toString());
                tvTotalTime.setText(userInfo.getTotalTime());
                tvTotalCalories.setText(userInfo.getTotalCalories() == null ? 0 + ""
                        : userInfo.getTotalCalories().toString());
                tvUserNumber.setText("ID:" + userInfo.getUserNumber());
                MyBitmapUtilsHelper.getInstance().getBitmapUtils().display(ivIcon, userInfo.getIcon());
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /**
     * 点击我的运动记录时跳转页面
     *
     * @param v
     */
    @OnClick(R.id.mine_tv_record)
    public void onMyRecorderClick(View v) {
        Intent intent = new Intent(getActivity(), RecorderActivity.class);
        intent.putExtra(UserInfo.class.getName(), userInfo);
        startActivity(intent);
    }

    public static class ViewPagerAdapter extends FragmentPagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return ChartFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return MAX_COUNT;
        }
    }


}
